import { Component, ViewChild } from '@angular/core';
import { Platform,ModalController,NavController,ToastController} from 'ionic-angular';
import { Storage } from '@ionic/storage';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

import { HomePage } from '../pages/home/home';
import { ModifyAvatar } from '../pages/modifyavatar/modifyavatar';
import { ModifyPassword } from '../pages/modifypassword/modifypassword';

@Component({
  selector: 'page-app',
  templateUrl: 'app.html'
})
export class MyApp {
  //@ViewChild(Nav) nav: Nav;
  @ViewChild('content') nav: NavController;
  public rootPage: any = HomePage;
  pages: Array<{title: string, component: any}>;

  constructor(
    public platform: Platform,
    public statusBar: StatusBar,
    public storage: Storage,
    public modalCtrl: ModalController,
    public toastCtrl: ToastController,
    public splashScreen: SplashScreen,
  ){
    this.initializeApp();

    // used for an example of ngFor and navigation
    this.pages = [
      { title: '修改密码', component: ModifyPassword },
      { title: '修改资料', component: ModifyAvatar },
      { title: '退出登录', component: HomePage }
    ];

  }
  private dismissHandler() {
    //console.info('Toast onDidDismiss()');
  }

  //消息提示函数
  formTip(hint){
    const toast = this.toastCtrl.create({
      message: hint,
      duration: 3000,
      position: 'middle'
    });
    toast.onDidDismiss(this.dismissHandler);
    toast.present();
    return;
  }

  initializeApp() {
    this.platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      this.statusBar.styleDefault();
      this.splashScreen.hide();
    });
  }

  openPage(page) {
    // Reset the content nav to have just this page
    // we wouldn't want the back button to show in this scenario
    //this.nav.setRoot(page.component);
    if(page.component == HomePage){
      this.storage.remove('mytoken')
      this.storage.remove('formcontent')
      this.storage.remove('select_form')
      this.storage.remove('pushid')
      this.formTip('您已退出登录！');
      this.nav.goToRoot(page.component);
    }else{
      this.nav.push(page.component)
    }
  }
}
